package com.bangcommunity.bbframe.sdm.utils;

import com.bangcommunity.bbframe.common.utils.lang.StringUtils;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.AntPathMatcher;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class ServletUtil {
    static Logger logger = LoggerFactory.getLogger(ServletUtil.class);
    private static AntPathMatcher antPathMatcher = new AntPathMatcher();

    public static boolean marchUrl(HttpServletRequest request, String express) {
        String requestUri = request.getRequestURI();
        if (StringUtils.isNotBlank(requestUri)) {
            int firstQuestionMarkIndex = requestUri.lastIndexOf("?");
            if (firstQuestionMarkIndex != -1) {
                requestUri = requestUri.substring(0, firstQuestionMarkIndex);
            }
        }
        return antPathMatcher.match(express, requestUri);
    }

    public static Map<String, String> readParam(HttpServletRequest request) {
        Enumeration<String> parameterNames = request.getParameterNames();
        Map<String, String> params = new HashMap<String, String>();
        if (null != parameterNames) {
            while (parameterNames.hasMoreElements()) {
                String name = parameterNames.nextElement();
                String parameter = request.getParameter(name);
                params.put(name, parameter);
            }
        }
        return params;
    }

    public static String dumpRequestParam(HttpServletRequest request) {
        if (isMultipart(request)) {
            return "uploadfile";
        }
        Enumeration<String> parameterNames = request.getParameterNames();
        StringBuffer sbuf = new StringBuffer();
        if (null != parameterNames) {
            while (parameterNames.hasMoreElements()) {
                String name = parameterNames.nextElement();
                sbuf.append(name);
                sbuf.append("=");
                String parameter = request.getParameter(name);
                if (parameter != null && parameter.length() > 32) {
                    parameter = parameter.substring(0, 32);
                }
                sbuf.append(parameter);
                sbuf.append(",");
            }
        }
        return sbuf.toString();
    }

    public static String dumpRequestBody(HttpServletRequest request) {
        if (isMultipart(request)) {
            return "uploadfile";
        }
        StringBuffer sbuf = new StringBuffer();
        BufferedReader reader = null;
        try {
            reader = request.getReader();
            String s = null;
            while (null != (s = reader.readLine())) {
                sbuf.append(s);
            }
        } catch (IOException e) {
            logger.error("dumpRequestBody error", e);
        } finally {
            if (null != reader) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
        return sbuf.toString();
    }

    public static String dumpRequestHeader(HttpServletRequest request) {
        if (isMultipart(request)) {
            return "uploadfile";
        }
        Enumeration<String> parameterNames = request.getHeaderNames();
        StringBuffer sbuf = new StringBuffer();
        if (null != parameterNames) {
            while (parameterNames.hasMoreElements()) {
                String name = parameterNames.nextElement();
                sbuf.append(name);
                sbuf.append("=");
                String parameter = request.getHeader(name);
                if (parameter != null && parameter.length() > 32) {
                    parameter = parameter.substring(0, 32);
                }
                sbuf.append(parameter);
                sbuf.append(",");
            }
        }
        return sbuf.toString();
    }

    public static String addParam(String url, Map<String, String> param) {
        if (StringUtils.isBlank(url) || param == null || param.isEmpty()) {
            return url;
        }
        StringBuffer sbuf = new StringBuffer();
        sbuf.append(url);
        if (url.indexOf("?") == -1) {
            sbuf.append("?");
        } else {
            sbuf.append("&");
        }
        boolean first = true;
        for (Map.Entry<String, String> e : param.entrySet()) {
            if (!first) {
                sbuf.append("&");
            }
            sbuf.append(e.getKey());
            sbuf.append("=");
            sbuf.append(e.getValue());
            first = false;
        }
        return sbuf.toString();
    }

    public static boolean isMultipart(HttpServletRequest request) {
        return (request != null && ServletFileUpload.isMultipartContent(request));
    }

    public static void main(String[] args) {
        System.out.println(antPathMatcher.match("/**/tese", ""));
    }
}
